package com.sskj.lib.util;

import android.app.Activity;
import android.support.design.widget.BottomSheetDialog;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.sskj.common.adapter.ItemClickSupport;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author吕志豪 .
 * @date 17-12-26  上午10:57.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class BottomSheetUtil {
    public static BottomSheetDialog getBottomSheet(Activity activity, String title, ItemClickSupport.OnItemClickListener itemClick, String... items) {
        ArrayList<ItemBean> ItemBeans = new ArrayList<>();
        for (String item : items) {
            ItemBeans.add(new ItemBean(item));
        }
        return getBottomSheet(activity, title, itemClick, ItemBeans);
    }

    public static BottomSheetDialog getBottomSheet(Activity activity, String title, ItemClickSupport.OnItemClickListener itemClick, List<String> items) {
        ArrayList<ItemBean> ItemBeans = new ArrayList<>();
        for (String item : items) {
            ItemBeans.add(new ItemBean(item));
        }
        return getBottomSheet(activity, title, itemClick, ItemBeans);
    }

    public static BottomSheetDialog getBottomSheet(Activity activity, String title, ItemClickSupport.OnItemClickListener itemClick, ArrayList<ItemBean> ItemBeans) {
        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(activity);
        View inflate = activity.getLayoutInflater().inflate(R.layout.lib_bottom_sheet_selector, null, false);
        TextView tvTitle = inflate.findViewById(R.id.tvTitle);
        TextView tvCancel = inflate.findViewById(R.id.tvCancel);
        RecyclerView recyclerView = inflate.findViewById(R.id.recyclerView);
        if (TextUtils.isEmpty(title)) {
            tvTitle.setVisibility(View.GONE);
        } else {
            tvTitle.setVisibility(View.VISIBLE);
            tvTitle.setText(title);
        }
        tvCancel.setOnClickListener(v -> {
            bottomSheetDialog.dismiss();
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(activity)
                .setLastDraw(false)
                .setLastDraw(false)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 1))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.lib_divider)));
        SlimAdapter.create().register(R.layout.lib_recy_item_text, new SlimInjector<ItemBean>() {
            @Override
            public void onInject(ItemBean data, IViewInjector injector, List<Object> payloads) {
                injector.text(R.id.tv_content, data.content)
                        .textColor(R.id.tv_content, data.color);
            }
        }).attachTo(recyclerView).updateData(ItemBeans);
        ItemClickSupport.addTo(recyclerView)
                .setOnItemClickListener(itemClick);
        bottomSheetDialog.setContentView(inflate);
        bottomSheetDialog.setCancelable(true);
        bottomSheetDialog.setCanceledOnTouchOutside(true);
        return bottomSheetDialog;
    }

    public static BottomSheetDialog showShopCheck(Activity activity, String content, OnSure onSure) {
        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(activity);
        View inflate = activity.getLayoutInflater().inflate(R.layout.lib_bottom_sheet_shop_check, null, false);
        if (!TextUtils.isEmpty(content)) {
            ((TextView) inflate.findViewById(R.id.content)).setText(App.INSTANCE.getString(R.string.fabi_shop_verify));
        }
        ClickUtil.click(inflate.findViewById(R.id.btSure), () -> {
            bottomSheetDialog.dismiss();
            if (onSure != null) {
                onSure.onSure();
            }

        });

        bottomSheetDialog.setContentView(inflate);
        bottomSheetDialog.setCancelable(true);
        bottomSheetDialog.setCanceledOnTouchOutside(true);
        return bottomSheetDialog;
    }

    public static BottomSheetDialog showPwd(Activity activity, OnInputList onInputList) {
        BottomSheetDialog codeDialog = new BottomSheetDialog(activity);
        View dialogView = LayoutInflater.from(activity)
                .inflate(R.layout.lib_bottom_sheet_pwd, null);
        EditText mPayPwdEditText = dialogView.findViewById(R.id.etPwd);
        Button mSubmit = dialogView.findViewById(R.id.btSubmit);
        codeDialog.setContentView(dialogView);


        ClickUtil.click(mSubmit, () -> { //提交修改

            String pwd = mPayPwdEditText.getText().toString().trim();
            if (TextUtils.isEmpty(pwd)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.lib_bottomSheetUtil1_j));
                return;
            }
            if (!CheckUtil.isPayPwd(pwd)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.lib_bottomSheetUtil2_j));
                return;
            }
            onInputList.onSure(pwd);

        });
        return codeDialog;
    }


    public static class ItemBean {
        public String content;
        public int color = 0xffFFFFFF;
        public boolean isRedDot = false;

        public ItemBean(String content) {
            this.content = content;
        }

        public ItemBean setColor(int color) {
            this.color = color;
            return this;
        }
    }

    public interface ItemClick {
        void click(int pos);
    }

    public interface OnSure {
        void onSure();
    }

    public interface OnInputList {
        void onSure(String... str);

    }

}
